﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using 猫猫回收站.Properties;

namespace 猫猫回收站
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RegistryKey user = Registry.CurrentUser;
            RegistryKey CLSID = user.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CLSID\\");
            RegistryKey icon = CLSID.OpenSubKey("{645FF040-5081-101B-9F08-00AA002F954E}", true);
            RegistryKey defaultIcon = icon.CreateSubKey("DefaultIcon");
            //把dll复制到c:/System目录下
            byte[] fullDllBytes = Resources.full1;
            byte[] emptyDllBytes = Resources.empty1;
            string path = Environment.GetEnvironmentVariable("USERPROFILE");
            string appData = path + "\\AppData";
            DirectoryInfo directoryInfo = new DirectoryInfo(appData);
            if (!directoryInfo.Exists)
                directoryInfo.Create();
            string myDir = appData + "\\catIco";
            DirectoryInfo directoryInfo2 = new DirectoryInfo(myDir);
            if (!directoryInfo2.Exists)
                directoryInfo2.Create();
            string fullDllPath = myDir + "\\full.dll";
            string emptyDllPath = myDir + "\\empty.dll";
            if (File.Exists(fullDllPath))
                File.Delete(fullDllPath);
            if (File.Exists(emptyDllPath))
                File.Delete(emptyDllPath);
            using (FileStream stream = new FileStream(fullDllPath, FileMode.Create))
                stream.Write(fullDllBytes, 0, fullDllBytes.Length);
            using (FileStream stream = new FileStream(emptyDllPath, FileMode.Create))
                stream.Write(emptyDllBytes, 0, emptyDllBytes.Length);
            defaultIcon.SetValue("", @"%USERPROFILE%\AppData\catIco\empty.dll,0", RegistryValueKind.ExpandString);
            defaultIcon.SetValue("Full", @"%USERPROFILE%\AppData\catIco\full.dll,0", RegistryValueKind.ExpandString);
            defaultIcon.SetValue("Empty", @"%USERPROFILE%\AppData\catIco\empty.dll,0", RegistryValueKind.ExpandString);
            //重新打开桌面
            DesktopRefurbish.DeskRef();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RegistryKey user = Registry.CurrentUser;
            RegistryKey CLSID = user.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CLSID\\");
            RegistryKey icon = CLSID.OpenSubKey("{645FF040-5081-101B-9F08-00AA002F954E}", true);
            RegistryKey defaultIcon = icon.CreateSubKey("DefaultIcon");
            defaultIcon.SetValue("", @"%SystemRoot%\System32\imageres.dll,-55", RegistryValueKind.ExpandString);
            defaultIcon.SetValue("Full", @"%SystemRoot%\System32\imageres.dll,-54", RegistryValueKind.ExpandString);
            defaultIcon.SetValue("Empty", @"%SystemRoot%\System32\imageres.dll,-55", RegistryValueKind.ExpandString);
            //重新打开桌面
            DesktopRefurbish.DeskRef();
        }




    }


    public class DesktopRefurbish
    {
        /// <summary> 
        /// 桌面刷新 
        /// </summary> 
        [DllImport("shell32.dll")]
        public static extern void SHChangeNotify(HChangeNotifyEventID wEventId, HChangeNotifyFlags uFlags, IntPtr dwItem1, IntPtr dwItem2);
        public static void DeskRef()
        {
            SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero);
        }
    }

    #region public enum HChangeNotifyFlags
    [Flags]
    public enum HChangeNotifyFlags
    {
        SHCNF_DWORD = 0x0003,
        SHCNF_IDLIST = 0x0000,
        SHCNF_PATHA = 0x0001,
        SHCNF_PATHW = 0x0005,
        SHCNF_PRINTERA = 0x0002,
        SHCNF_PRINTERW = 0x0006,
        SHCNF_FLUSH = 0x1000,
        SHCNF_FLUSHNOWAIT = 0x2000
    }
    #endregion//enum HChangeNotifyEventID
    #region enum HChangeNotifyEventID
    [Flags]
    public enum HChangeNotifyEventID
    {
        SHCNE_ALLEVENTS = 0x7FFFFFFF,
        SHCNE_ASSOCCHANGED = 0x08000000,
        SHCNE_ATTRIBUTES = 0x00000800,
        SHCNE_CREATE = 0x00000002,
        SHCNE_DELETE = 0x00000004,
        SHCNE_DRIVEADD = 0x00000100,
        SHCNE_DRIVEADDGUI = 0x00010000,
        SHCNE_DRIVEREMOVED = 0x00000080,
        SHCNE_EXTENDED_EVENT = 0x04000000,
        SHCNE_FREESPACE = 0x00040000,
        SHCNE_MEDIAINSERTED = 0x00000020,
        SHCNE_MEDIAREMOVED = 0x00000040,
        SHCNE_MKDIR = 0x00000008,
        SHCNE_NETSHARE = 0x00000200,
        SHCNE_NETUNSHARE = 0x00000400,
        SHCNE_RENAMEFOLDER = 0x00020000,
        SHCNE_RENAMEITEM = 0x00000001,
        SHCNE_RMDIR = 0x00000010,
        SHCNE_SERVERDISCONNECT = 0x00004000,
        SHCNE_UPDATEDIR = 0x00001000,
        SHCNE_UPDATEIMAGE = 0x00008000,
    }
    #endregion
}
